package com.freeman.cement.entity;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonInclude;
import  com.freeman.cement.exception.AppExceptionCodeMsg;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author 66
 * @param <T>
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ResponseResult<T> {
    /**
     * 状态码
     */
    private int code;
    /**
     * 提示信息，如果有错误时，前端可以获取该字段进行提示
     */
    private String msg;
    /**
     * 查询到的结果数据，
     */
    private T data;

    /**
     * 成功返回
     *
     * @param data
     * @param <T>
     * @return
     */
    public static <T> ResponseResult success(T data) {
        ResponseResult resp = new ResponseResult(200, "success", data);
        return resp;
    }

    /**
     * 成功返回及自定义消息
     *
     * @param msg
     * @param data
     * @param <T>
     * @return
     */
    public static <T> ResponseResult success(String msg, T data) {
        ResponseResult resp = new ResponseResult(200, msg, data);
        return resp;
    }

    /**
     * 返回成功的自定义消息
     * @param msg
     * @return
     */
    public static ResponseResult successMsg(String msg) {
        ResponseResult resp = new ResponseResult(200, msg, null);
        return resp;
    }

    /*   *//**
     * 返回空对象
     * @return
     *//*
    public static ResponseResult successNull() {
        ResponseResult resp = new ResponseResult(200, "success", new JSONObject());
        return resp;
    }*/

    /**
     * 用于返回自定义的运行异常
     *
     * @param appExceptionCodeMsg
     * @param <T>
     * @return
     */
    public static <T> ResponseResult error(AppExceptionCodeMsg appExceptionCodeMsg) {
        ResponseResult resp = new ResponseResult(appExceptionCodeMsg.getCode(), appExceptionCodeMsg.getMsg(), null);
        return resp;
    }

    /**
     * 用于返回未定义的系统异常
     *
     * @param code
     * @param msg
     * @param <T>
     * @return
     */
    public static <T> ResponseResult error(int code, String msg) {
        ResponseResult resp = new ResponseResult(code, msg, null);
        return resp;
    }

}